<?php
    @session_start();
    require_once 'shared-functions.php';
    require_once 'session.php';
    require_once 'masterpage.php';

    if(!IsValidSession())
    {
        header('Location: login.php?page=index');
        exit();
    }
    else
    {
        RefreshSession();
    }

    $user = GetCurrentUserAccessLevel();

    //Check user access
    if($user != $DIRECTOR && $user != $ADMIN && $user != $PRINCIPLE)
    {
        header('Location: login.php?page=add-new-semester&error=To access the add new semester page, please log in as a director, principle or admin&logout=1');
        exit();
    }

    masterpage("Add New Semester");

	$semesterDesc = "";
	$datepicker1 = "";
	$datepicker2 = "";
	$omitForm = false;
	if(isset($_POST['semesterDesc']) && isset($_POST['datepicker1']) && isset($_POST['datepicker2']) && isset($_POST['programID']))
	{
		$semesterDesc = $_POST["semesterDesc"];
		$datepicker1 = $_POST["datepicker1"];	
		$datepicker2 = $_POST["datepicker2"];
		$programID = $_POST["programID"];
	}

	// variable to hold the list of courses 
	$programs = "";
	
	$link = connect_db();
	
	$programQuery = "select ProgramID, Name from Program";

	$programQueryResult = 	mysql_query($programQuery, $link);

    	if($programQueryResult==false)
	{
		$_SESSION['error']="Could not get program details. - add-new-semester.php";
		return false;
	}
	else
	{
		while($currentProgramsRow = mysql_fetch_array($programQueryResult, MYSQL_ASSOC))
		{
			$programID = $currentProgramsRow["ProgramID"];
			$name = $currentProgramsRow["Name"];
			$programs .= "<option value='$programID'>$name</option>";
		}
	}


    if (strlen($semesterDesc) > 0 && strlen($datepicker1) > 0 && strcmp($datepicker1,"--") != 0 &&strlen($datepicker2) > 0 && strcmp($datepicker2,"--") != 0 && strlen($programID) > 0)
    {
	try
	{
		$newStartDate = ParseDateValues($datepicker1);
		$newEndDate = ParseDateValues($datepicker2);
		
		$insertQuery = "insert into Semester (StartDate, EndDate, Description, ProgramID) values ('$newStartDate', '$newEndDate', '$semesterDesc','$programID')";
		$result = mysql_query($insertQuery, $link);
		if($result===false)
		{
			$_SESSION['error']="Could not insert new semester. - add-new-semester.php";
			echo'<span style="color:red;">Could not insert into database.'.mysql_error().'</span>';
		}
		else
		{
		    echo "<a href='manage-semesters.php'>Return to Manage Semesters</a>";	

		    $omitForm = true;		
		}
	}
	catch (Exception $exception)
	{
		$_SESSION['error']=$exception->getMessage();
		return false;
	}
    }
    else
    {
    }
?>

<link type="text/css" href="jquery/css/overcast/jquery-ui-1.8.9.custom.css" rel="stylesheet" />	
	<script type="text/javascript" src="jquery/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="jquery/js/jquery-ui-1.8.9.custom.min.js"></script>
	<script type="text/javascript">
		$(function(){

			// Dialog			
			$('#dialog').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
					"Ok": function() { 
						$(this).dialog("close"); 
					}, 
					"Cancel": function() { 
						$(this).dialog("close"); 
					} 
				}
			});
		
			// Dialog Link
			$('#dialog_link').click(function(){
				$('#dialog').dialog('open');
				return false;
			});

			// Datepicker1
			$('#datepicker1').datepicker({
				dateFormat: 'dd-mm-yy',
				width: '100px',
				inline: true,
				changeMonth: true,
				changeYear: true
			});

			// Datepicker2
			$('#datepicker2').datepicker({
				dateFormat: 'dd-mm-yy',
				inline: true,
				changeMonth: true,
				changeYear: true
			});
		
			//hover states on the static widgets
			$('#dialog_link, ul#icons li').hover(
				function() { $(this).addClass('ui-state-hover'); }, 
				function() { $(this).removeClass('ui-state-hover'); }
			);
		
		});
	</script>
	<script type="text/javascript">
		$(document).ready(function() {
			// $('#formLocation').load('ajax/add-semester-form.html');
		});
		

		function completeForm()
		{
			var semesterDescription = document.getElementByID('semesterDesc');
			var program = document.getElementByID('programID');
			var startDate = document.getElementByID('datepicker1');
			var endDate = document.getElementByID('datepicker2');
			
			if((semesterDescription.value == "") 
				|| (program.value == "") 
				|| (startDate.value == "") 
				|| (endDate.value == ""))
			{

				return false;
			}
			else
			{
				return true;
			}
		}
	</script>
	<style type="text/css">
		div.ui-datepicker{ font-size:12px;}
	</style>

<?php
    if (!$omitForm)
    {
?>
    <form action="add-new-semester.php" method="post">
	    <input type="hidden" name="isSubmitted" value="true">
	    <table class="DataList">
		<thead>
			<tr>
				<th class="header" colspan="2">Add Semester</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><span style='color:red;'>All fields are required.</span></td>
			</tr>
			<tr>
				<td class="rows">Semester Description:</td>
				<td class="rows"><input type="text" id="semesterDesc" name="semesterDesc" value="<?php echo $semesterDesc; ?>"><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="rows">Program:</td>
				<td class="rows">
				    <select id="programID" name="programID" size="4" style="width:250px">
					<?php echo $programs; ?>
				    </select><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="rows">Start Date:</td>
				<td class="rows"><input type="text" id="datepicker1" name="datepicker1" value="<?php echo DisplayDate($datepicker1); ?>"><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="rows">End Date:</td>
				<td class="rows"><input type="text" id="datepicker2" name="datepicker2" value="<?php echo DisplayDate($datepicker2); ?>"><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="rows" style="text-align:right;"><input type="submit" value="Submit" onclick="completeForm()"></td>
				<td class="rows"><input type="reset" value="Clear"></td>
			</tr>
		</tbody>
	    </table>
    </form>
<?php	
}
	endmasterpage();
?>
